package com.pz.cloud.framework.business.workflow.aspect;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.pz.cloud.framework.base.model.WorkflowEntity;
import com.pz.cloud.framework.business.workflow.annotation.Workflow;
import com.pz.cloud.framework.business.workflow.entity.WaitDoneUser;
import com.pz.cloud.framework.business.workflow.model.BaseTaskCallback;
import com.pz.cloud.framework.business.workflow.model.ProcessRecordModel;
import com.pz.cloud.framework.business.workflow.model.WorkflowModel;
import com.pz.cloud.framework.business.workflow.service.IProcessBindService;
import com.pz.cloud.framework.business.workflow.service.IWaitDoneService;
import com.pz.cloud.framework.business.workflow.service.IWorkflowService;
import com.pz.cloud.framework.util.BizUtil;
import com.pz.cloud.framework.util.WorkflowUtil;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.SpringContextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.Method;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 审批记录切面类
 * @author mzz
 */
@Aspect
@Order(1)
@Component
public class ApproveRecordAspect {


    @Autowired
    private IWaitDoneService waitDoneService;


    @Around("@annotation(com.pz.cloud.framework.business.workflow.annotation.AssembleApproveRecord)")
    public Object submit(ProceedingJoinPoint joinPoint) throws Throwable{
        Object result = null;
        result = joinPoint.proceed();
        List<ProcessRecordModel> approveRecords;
        if(result instanceof WorkflowEntity){
            WorkflowUtil.assembleDealFlag((WorkflowEntity) result);
        }else if(result instanceof Result){
            Object r = ((Result) result).getResult();
            if(r instanceof  WorkflowEntity){
                approveRecords = waitDoneService.findSignleRecordByBizId(BizUtil.getIdFieldValue(r));
                ((WorkflowEntity) r).setApproveRecords(approveRecords);
            }else if(r instanceof IPage){
                List records = ((IPage) r).getRecords();
                if(records!=null){
                    for (Object record : records) {
                        if(record instanceof WorkflowEntity){
                            approveRecords = waitDoneService.findSignleRecordByBizId(BizUtil.getIdFieldValue(r));
                            ((WorkflowEntity) record).setApproveRecords(approveRecords);
                        }
                    }
                }
            }
        }
        return result;
    }
}